import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class JsonDemo {
    protected static void demoLogic() {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        //System.out.println("gson : "+gson);

        ArrayList<String> monthArr = new ArrayList<String>(Arrays.asList(
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"));
        HashMap<String, Integer> monthDic = new HashMap<String, Integer>() {
            // 第一层括弧实际是定义了一个匿名内部类 (Anonymous Inner Class)
            {
                // 第二层括弧实际上是一个实例初始化块 (instance initializer block)，这个块在内部匿名类构造时被执行

                put("January", 31);
                put("February", 28);
                put("March", 31);
                put("April", 30);
                put("May", 31);
                put("June", 30);
                put("July", 31);
                put("August", 31);
                put("September", 30);
                put("October", 31);
                put("November", 30);
                put("December", 31);
            }
        };

        HashMap<String,Object> dic = new HashMap<String,Object>() {
            // 第一层括弧实际是定义了一个匿名内部类 (Anonymous Inner Class)
            {
                // 第二层括弧实际上是一个实例初始化块 (instance initializer block)，这个块在内部匿名类构造时被执行
                put("arr", monthArr);
                put("dic", monthDic);
                put("str","zhang");
            }
        };

        // array --》json
        String strJson = gson.toJson(monthArr);
        System.out.println("monthArr Json "+strJson);

        // dic --》json
        strJson = gson.toJson(monthDic);
        System.out.println("monthDic Json "+strJson); // 无法得到有效json
        Type dicType = new TypeToken<HashMap<String, Integer>>(){}.getType();
        strJson = gson.toJson(monthDic, dicType);
        System.out.println("monthDic Json with type "+strJson); // 得到有效json

        // 复杂dic --》json
        strJson = gson.toJson(dic);
        System.out.println("dic Json "+strJson); // 无法得到有效json

        Type dicType2 = new TypeToken<HashMap<String,Object>>(){}.getType();
        strJson = gson.toJson(dic, dicType2);
        System.out.println("dic Json with type "+strJson); // 得到有效json，但是不完整


        DemoPerson p1 = new DemoPerson("1LiLei",      'M', 12, "505_class-1-1");
        DemoPerson p2 = new DemoPerson("2HanMeiMei",  'F', 13, "504_class-1-2");
        DemoPerson p3 = new DemoPerson("3JimGreen",   'M', 11, "503_class-1-3");

        // 对象实例 --》json
        strJson = gson.toJson(p1);
        System.out.println("DemoPerson Json "+strJson);
        // 对象实例《-- json
        DemoPerson personFromJson = gson.fromJson(strJson, DemoPerson.class);
        System.out.println("personFromJson "+personFromJson);

        // 对象实例Array --》json
        ArrayList<DemoPerson> personArr = new ArrayList<DemoPerson>(Arrays.asList(p1,p2,p3));
        strJson = gson.toJson(personArr);
        System.out.println("personArr Json "+strJson);

        // 对象实例Array 《--json
        Type personArrType = new TypeToken<ArrayList<DemoPerson>>(){}.getType();
        ArrayList<DemoPerson> arrFromJson = gson.fromJson(strJson, personArrType);
        for (DemoPerson obj : arrFromJson) {
            System.out.println("obj from Json "+obj);
        }
    }
}
